import Http from '../../utils/http.js';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseSize: true,
    animationData: {},
    loading: false,
    list: [],//列表数据
    curName: '',//列表数据第一项
  },
  chooseClick: function (e) {
    console.log(1)
  },
  showChoose: function (e) {
    // 用that取代this，防止不必要的情况发生
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 300,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      chooseSize: true
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 100)
  },

  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(200).step()
    that.setData({
      animationData: animation.export()

    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),
        chooseSize: false
      })
    }, 200)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTechanList()
  },
  getTechanList() {
    this.setData({
      loading: true
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    Http.Request('GET', `/api/customer/commodity/getSpecialProductPage/v1`).then(res => {
      this.setData({
        loading: false
      })
      wx.hideLoading();
      this.formatList(res)
    }).catch(err => {
      wx.hideLoading();
      app.globalFn.reLoad(this.getTechanList, err)
    })
  },
  formatList(arr) {
    let list = [];
    let obj = {};
    arr.forEach((o, i) => {
      obj[i] = [{ name: o.groupName }];
      if (o.list && o.list.length) {
        obj[i] = obj[i].concat(o.list)
      }
    })
    list.push(obj);
    const curName = list[0][0][0].name;
    this.setData({
      list,
      curName
    })
  },
  //点击列表商品
  itemClick(e){
    const commodityCode = e.detail.commodityCode;
    wx.navigateTo({
      url: `./shiwuDetail/shiwuDetail?commodityCode=${commodityCode}`
    })
  },
  onShareAppMessage() {
    app.globalFn.onShareAppMessage()
  }
})